package cn.vc.content.controller.video;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.content.entity.video.VideoHistory;
import cn.vc.content.entity.video.dto.VideoDto;
import cn.vc.content.service.video.VideoHistoryService;
import cn.vc.content.service.video.VideoService;
import cn.vc.content.util.SecurityUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 视频历史接口
 */
@RestController
@RequestMapping("/videohistory")
//@PreAuthorize("hasAnyAuthority('ordinary')")
public class VideoHistoryController {
    @Resource
    private VideoService videoService;
    @Resource
    private VideoHistoryService videoHistoryService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询视频历史记录
     */
    @GetMapping
    public Result<List<VideoDto>> selectMyVideoHistory() {
        VUser vUser = SecurityUtil.getUser();
        if (vUser==null) return Result.NOT_LOGGED_IN();
        VideoHistory videoHistory = videoHistoryService.selectMyVideoHistory(vUser.getId());
        if (videoHistory==null) {
            return new Result<>(1, null, "查询成功");
        } else {
            List<Long> videoIdList = videoHistory.getVideoIdList();
            Collections.reverse(videoIdList);
            return new Result<>(1, this.videoService.selectByIds(videoIdList), "查询成功！");
        }
    }
}
